/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package formula.one.urjc.principal.view;

import formula.one.urjc.Metodos;
import formula.one.urjc.escuderia.Escuderia;
import javax.swing.JOptionPane;

/**
 *
 * @author santiago
 */
public class Login extends javax.swing.JFrame {

    Metodos metodo = new Metodos() ;

    /**
     * Creates new form Login
     */
    public Login() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        usuarioLogin = new javax.swing.JTextField();
        claveLogin = new javax.swing.JPasswordField();
        aceptarLogin = new javax.swing.JButton();
        cancelarLogin = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        perfilLogin = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Usuario:");

        jLabel2.setText("Contraseña:");

        usuarioLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                usuarioLoginActionPerformed(evt);
            }
        });

        aceptarLogin.setText("Entrar");
        aceptarLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aceptarLoginActionPerformed(evt);
            }
        });

        cancelarLogin.setText("Cancelar");
        cancelarLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelarLoginActionPerformed(evt);
            }
        });

        jLabel3.setText("Perfil:");

        perfilLogin.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Administrador", "Escudería", "Director" }));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(118, 118, 118)
                        .addComponent(aceptarLogin)
                        .addGap(52, 52, 52)
                        .addComponent(cancelarLogin))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(usuarioLogin)
                            .addComponent(claveLogin)
                            .addComponent(perfilLogin, 0, 154, Short.MAX_VALUE))))
                .addContainerGap(81, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(50, 50, 50)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(perfilLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usuarioLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(claveLogin, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 34, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(aceptarLogin)
                    .addComponent(cancelarLogin))
                .addGap(55, 55, 55))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void usuarioLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_usuarioLoginActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_usuarioLoginActionPerformed

    private void cancelarLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelarLoginActionPerformed
        // TODO add your handling code here:
        this.dispose() ;
    }//GEN-LAST:event_cancelarLoginActionPerformed

    private void aceptarLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aceptarLoginActionPerformed
        // TODO add your handling code here:
        // Obtenemos el Perfil
        String perfilGestion = (String) perfilLogin.getSelectedItem() ;
        // Obtenemos valores de usuario / contraseña
        String usuario = usuarioLogin.getText() ;
        String clave = new String(claveLogin.getPassword()) ;
        System.out.println(perfilGestion + " " + usuario + " " + clave) ;
        // Valor login correcto
        boolean loginOk = false ;
        if (perfilGestion.equals("Administrador") && usuario.equals("Administrador") &&
            clave.equals("Administrador")) {
            System.out.println("Abrir ventanas") ;
            loginOk = true ;
            ventanaAdministrador vA = new ventanaAdministrador() ;
            vA.setLocationRelativeTo(null);
            vA.setVisible(true) ;
            dispose() ;
        }
        if (perfilGestion.equals("Escudería")) {
            System.out.println("Recorremos fichero escuderias") ;
            /*Escuderia datosEscuderia ;
            datosEscuderia = metodo.getEscuderia("Escuderia") ;
            System.out.println("Propietario : " + datosEscuderia.getPropietario()) ;*/
            
            //loginOk = metodo.nombroEscuderias(usuario, clave) ;
            Escuderia esc=metodo.nombroEscuderias(usuario, clave);
            //if (loginOk) {
            //si devuelve una escudería el user y la clave son correctos
            if(esc!=null){
                loginOk=true;
                //se le pasa como parámetro la escudería correspondiente
                ventanaDuenoEscuderia vDE = new ventanaDuenoEscuderia(esc) ;
                vDE.setLocationRelativeTo(null);
                vDE.setTitle(esc.getNombre());
                vDE.setVisible(true) ;
                dispose() ;
            }
        }
        if (perfilGestion.equals("Director") && perfilGestion.equals("Director")) {
            System.out.println("Abrir ventanas") ;
            loginOk = true ;
            ventanaDirector vD = new ventanaDirector() ;
            vD.setLocationRelativeTo(null);
            vD.setVisible(true) ;
            dispose() ;
        }        
        if (loginOk == false) {
            JOptionPane.showMessageDialog(null, "Usuario / Contraseña incorrectas") ;
        }
    }//GEN-LAST:event_aceptarLoginActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(Login.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                Login vistaLogin=new Login();
                //que aparezca en el centro de l apantalla
                vistaLogin.setLocationRelativeTo(null);
                vistaLogin.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aceptarLogin;
    private javax.swing.JButton cancelarLogin;
    private javax.swing.JPasswordField claveLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JComboBox perfilLogin;
    private javax.swing.JTextField usuarioLogin;
    // End of variables declaration//GEN-END:variables
}
